# -*- coding: utf-8 -*-
"""
Created on Fri Jan 11 08:30:56 2013

@author: nzhao
"""


from traits.api import HasTraits, Dict, Str,  Array, List, Int, File
from traitsui.api import View, Item, Group, ListEditor, InstanceEditor

from chaco.api import ArrayPlotData
from numpy import array

from read_datafile import Read_File


default_para_contrl={}
default_face_contrl={'color':'red','type': 'line_scatter'}
    
class CurveData(HasTraits):
    
    xlist = Array()
    ylist = Array()
    
    name = Str()
    mode = Str()
    para_contrl = Dict()
    face_contrl = Dict()
    
    data_file = File('demo_data.dat')
    
    def __init__(self, x=array([0.0]), y=array([0.0]), name='origin', mode='dynamic', 
                 parameters=default_para_contrl, face=default_face_contrl, filename='demo_data.dat'):
        self.xlist = x
        self.ylist = y
        self.name = name
        self.mode = mode
        self.para_contrl = parameters
        self.face_contrl = face
        
        for k in self.para_contrl.keys():
            self.add_trait(k, self.para_contrl[k])
        for k in self.face_contrl.keys():
            self.add_trait(k, self.face_contrl[k])
        
        if mode is 'file':
            self.data_file = filename
            xy = Read_File(filename)
            self.xlist = xy[0]
            self.ylist = xy[1]
        elif mode is 'dynamic':
            self.kernerl()

            
    def _anytrait_changed(self, name, old, new):
        if name in self.para_contrl.keys():
            self.update_data()
        else:
            pass
        
    def update_data(self):
        self.kernerl()
    
    def kernerl(self):
        pass

        
    def default_traits_view(self):
        if self.mode is 'dynamic':
            items = [Item('name', style = 'readonly')] + [Item(k) for k in self.para_contrl.keys()]
        elif self.mode is 'file':
            items = [Item('name', style = 'readonly')] + [Item('data_file')]
        
        v=View()
        v.set_content(items)
        return v

class MyArrayPlotData(ArrayPlotData):
    curves = List(CurveData)
    curve_num = Int(0)
    
    curve_face= Dict()
   
    def __init__(self):
        super(ArrayPlotData, self).__init__()
        default_curve = CurveData()
        self.set_data(default_curve.name+'_x', default_curve.xlist)
        self.set_data(default_curve.name+'_y', default_curve.ylist)
        
    def add_curve(self, c):
        if isinstance(c, CurveData):
            self.curves.append( c )
            self.set_data(c.name+'_x', c.xlist)
            self.set_data(c.name+'_y', c.ylist)
            self.curve_num = len(self.curves)
            c.on_trait_change(self.update_plot_data, ['xlist','ylist'])
        elif isinstance(c, list):
            print 'list recieved'
            pass

    def update_plot_data(self, c,  name, new):
        self.set_data(c.name+'_x', c.xlist)
        self.set_data(c.name+'_y', c.ylist)
    

    traits_view = View(Group(
            Item('curves', editor=ListEditor(rows=10, editor=InstanceEditor()), style='readonly', show_label=False),
            label='curves', show_border=True
            ),
            )
        
        